package com.hnichr.crm.biz.contacts.controller;

import com.hnichr.crm.common.contacts.entity.CrmContacts;
import com.hnichr.crm.common.contacts.service.ICrmContactsService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * 联系人Controller
 *
 * @author Cheng
 * @date 2022-11-24
 */
@Slf4j
@Tag(name = "联系人Controller", description = "联系人")
@RestController()
@RequestMapping("/contacts")
public class CrmContactsController {

    @Autowired
    private ICrmContactsService crmContactsService;

    /**
     * 查询联系人列表
     */
    @GetMapping("/list")
    @Operation(summary = "联系人", description = "分页查询", method = "GET")
    public Result<Page<CrmContacts>> list(Criteria criteria,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                          @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) throws Exception {
        Pageable pageAble = PageRequest.of(pageNo - 1, pageSize);
        Page<CrmContacts> page = crmContactsService.page(criteria, pageAble);
        return Result.OK(page);
    }

    /**
     * 添加
     */
    @PostMapping(value = "/add")
    @Operation(summary = "联系人-添加", description = "联系人-添加", method = "POST")
    public Result<?> add(@RequestBody CrmContacts crmContacts) {
        crmContacts.setBatchId(UUID.randomUUID().toString());
        if (StringUtils.isEmpty(crmContacts.getOwnerUserId())) {
            crmContacts.setOwnerUserId(SecurityUtils.getLoginUsername());
        }
        crmContactsService.save(crmContacts);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param crmContacts
     * @return
     */
    @Operation(summary = "联系人-编辑", description = "联系人-编辑", method = "PUT")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CrmContacts crmContacts) {
        crmContactsService.update(crmContacts);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @Operation(summary = "联系人-通过id删除", description = "联系人-通过id删除")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        crmContactsService.delete(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    @Operation(summary = "联系人-批量删除", description = "联系人-批量删除")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    @Operation(summary = "联系人-通过id查询", description = "联系人-通过id查询")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        CrmContacts crmContacts = crmContactsService.getById(id);
        if (crmContacts == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(crmContacts);
    }


}
